package com.whfc.ms.api.hdc.controller;

import com.whfc.common.enums.DeviceType;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.hdc.dto.AppDevicePlatformDTO;
import com.whfc.hdc.service.AppDevicePlatformService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-12-01 14:03
 */
@RestController
@RequestMapping("/ms/api/hdc/platform")
public class AppDevicePlatformController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = AppDevicePlatformService.class, version = "1.0.0")
    private AppDevicePlatformService platformService;

    @GetMapping("deviceType")
    public Result deviceTypes() {
        logger.info("查询所有硬件类型");
        DeviceType[] types = DeviceType.values();
        return ResultUtil.success(new ListData<>(Arrays.asList(types)));
    }

    @GetMapping("list")
    public Result listAll() {
        logger.info("查询所有硬件平台列表");
        Map<Integer, List<AppDevicePlatformDTO>> data = platformService.getDevicePlatform();
        return ResultUtil.success(data);
    }

    @GetMapping(value = "list", params = {"deviceType"})
    public Result listByDeviceType(@RequestParam("deviceType") Integer deviceType) {
        logger.info("查询硬件平台列表,deviceType:{}", deviceType);
        List<AppDevicePlatformDTO> list = platformService.getDevicePlatform(deviceType);
        return ResultUtil.success(new ListData<>(list));
    }

    @GetMapping(value = "list", params = {"deviceType", "pageNum", "pageSize"})
    public Result pageList(@RequestParam("deviceType") Integer deviceType, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        logger.info("查询硬件平台列表,deviceType:{},pageNum:{},pageSize:{}", deviceType, pageNum, pageSize);
        PageData<AppDevicePlatformDTO> data = platformService.getDevicePlatform(deviceType, pageNum, pageSize);
        return ResultUtil.success(data);
    }

    @RequiresPermissions(" ")
    @PostMapping("add")
    public Result add(@RequestBody AppDevicePlatformDTO param) {
        logger.info("新增硬件平台,param:{}", param);
        platformService.addDevicePlatform(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("hdc/platform/edit")
    @PostMapping("edit")
    public Result edit(@RequestBody AppDevicePlatformDTO param) {
        logger.info("新增硬件平台,param:{}", param);
        platformService.editDevicePlatform(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("hdc/platform/delete")
    @PostMapping("del")
    public Result del(@RequestBody AppDevicePlatformDTO param) {
        logger.info("删除硬件平台,param:{}", param);
        platformService.delDevicePlatform(param.getDeviceType(), param.getPlatform());
        return ResultUtil.success();
    }
}
